package fx.mine.active.component.blog.api;

import com.dtflys.forest.Forest;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        SubscribeAPI client = Forest.client(SubscribeAPI.class);
        String s = client.blogList();

        Document document = Jsoup.parse(s);
//        Elements elements = document.select("#daily > div > div.daily > h3");
        Elements select = document.select("#daily > div > .posts > .post");
        List<SubscribeAPI.Subscribe> list = parseSubscribe(select);
        System.out.println(list);
    }

    private static List<SubscribeAPI.Subscribe> parseSubscribe(Elements elements) {
        if (elements == null) {
            return Collections.emptyList();
        }
        List<SubscribeAPI.Subscribe> list = new ArrayList<>();

        for (Element element : elements) {
            Elements select1 = element.select(".post > .content");
            String dataUrl = select1.attr("data-url");
            String title = element.select(".post > .content > h3 > a").get(0).text();
            String authorName = element.select(".post > .user-info > .info > h4").get(0).text();
            String avatarUrl = element.select(".post > .user-info > .user-avatar > a > img").attr("src");
            SubscribeAPI.Subscribe subscribe = new SubscribeAPI.Subscribe();
            subscribe.setTitle(title);
            subscribe.setAuthorName(authorName);
            subscribe.setUserAvatar(avatarUrl);
            subscribe.setDataUrl(dataUrl);
            list.add(subscribe);
        }
        return list;
    }

    public String today(Document document) {
        Elements select = document.select("#daily > div > h3 > small");
        return select.get(0).text();
    }

}
